﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MapControl;

namespace GPSMap
{
    public partial class frmTestGPSData : Form
    {
        public frmGPSTracking formMain;
        private DataTable dbTestGPS;

        public bool isStartTestGPSData = false;
        private GPSPoint currGPSData;

        public frmTestGPSData(frmGPSTracking frm)
        {
            formMain = frm;
            InitializeComponent();
            InitTestGPSData();
        }

        private void InitTestGPSData()
        {
            formMain.mapControl.AxMap_MouseDownEvent += new AxMapXLib.CMapXEvents_MouseDownEventHandler(AxMap_MouseDownEvent);
            formMain.mapControl.AxMap_DrawUserLayer += new AxMapXLib.CMapXEvents_DrawUserLayerEventHandler(AxMap_DrawUserLayer);

            dbTestGPS = new DataTable();
            DataColumn col = new DataColumn();
            col = dbTestGPS.Columns.Add("LocalTime", typeof(string));
            col = dbTestGPS.Columns.Add("Name", typeof(string));
            col = dbTestGPS.Columns.Add("Lon", typeof(string));
            col = dbTestGPS.Columns.Add("Lat", typeof(string));
            col = dbTestGPS.Columns.Add("Alt", typeof(string));
            col = dbTestGPS.Columns.Add("Speed", typeof(string));
            col = dbTestGPS.Columns.Add("Angle", typeof(string));

            dataGridViewTestGPSData.DataSource = dbTestGPS;
            dataGridViewTestGPSData.Columns[0].HeaderText = "Thời gian";
            dataGridViewTestGPSData.Columns[1].HeaderText = "Tên";
            dataGridViewTestGPSData.Columns[2].HeaderText = "Kinh độ";
            dataGridViewTestGPSData.Columns[3].HeaderText = "Vĩ độ";
            dataGridViewTestGPSData.Columns[4].HeaderText = "Độ cao";
            dataGridViewTestGPSData.Columns[5].HeaderText = "Tốc độ";
            dataGridViewTestGPSData.Columns[6].HeaderText = "Hướng";
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            isStartTestGPSData = !isStartTestGPSData;
            if (isStartTestGPSData)
            {
                btnTest.Text = "Stop Test";
            }
            else
            {
                btnTest.Text = "Start Test";
            }
        }

        public void AxMap_MouseDownEvent(object sender, AxMapXLib.CMapXEvents_MouseDownEvent e)
        {
            PointF pScreen = new PointF(e.x, e.y);
            Graphics g = formMain.mapControl.Map.CreateGraphics();
            
            if (e.button == 1)
            {
                //Nếu là đang thêm dữ liệu mới
                if (isStartTestGPSData)
                {
                    formMain.mapControl.Map.CurrentTool = MapXLib.ToolConstants.miArrowTool;

                    //Hiển thị tọa độ Longitude, Latitude
                    double lon, lat;
                    lon = lat = 0;
                    formMain.mapControl.Map.ConvertCoord(ref e.x, ref e.y, ref lon, ref lat, MapXLib.ConversionConstants.miScreenToMap);

                    DataRow row = dbTestGPS.NewRow();
                    Random ran = new Random();
                    row.ItemArray = new object[] { DateTime.Now.ToString(), "Heli1", lon.ToString(), lat.ToString(), ran.Next(2000, 3000).ToString("#,#"), ran.Next(200, 400).ToString(), ran.Next(0, 360).ToString() };
                    dbTestGPS.Rows.Add(row);
                    //Tọa độ hiện tại cũ vẽ lại màu xanh
                    if (currGPSData != null)
                        currGPSData.Draw(g, Color.Blue);

                    currGPSData = GetGPSData(row);
                    currGPSData.Draw(g, Color.Red);
                }
                else //Nếu đang chọn xem thông tin gps data
                {
                    for (int i = 0; i < dataGridViewTestGPSData.Rows.Count; i++)
                    {
                        GPSPoint objGPS = GetGPSData(dbTestGPS.Rows[i]);
                        //Nếu click trúng vào vị trí GPS thì hiển thị form
                        if (objGPS.HitTest(pScreen) == 0 &&
                            formMain.mapControl.Map.CurrentTool == MapXLib.ToolConstants.miArrowTool)
                        {
                            dataGridViewTestGPSData.Rows[i].Selected = true;
                            //Tọa độ hiện tại cũ vẽ lại màu xanh
                            currGPSData.Draw(g, Color.Blue);
                            //Gán tọa độ hiện tại mới
                            currGPSData = objGPS;
                            //Tô màu đỏ
                            currGPSData.Draw(g, Color.Red);
                            //formMain.mapControl.Refresh();
                            string result = string.Format("Tên = {0}\n Kinh độ = {1}\n Vĩ độ = {2}\n Độ cao= {3}\n Tốc độ = {4}\n Hướng={5}",
                                objGPS.Name,
                                objGPS.LonGPS.ToString(),
                                objGPS.LatGPS.ToString(),
                                objGPS.Alt.ToString("#,#"),
                                objGPS.Speed.ToString(),
                                objGPS.Angle.ToString()
                                );
                            MessageBox.Show(result);
                        }
                    }

                }
            }
        }

        

        

        private void AxMap_MouseWheelEvent(object sender, AxMapXLib.CMapXEvents_MouseWheelEvent e)
        {

        }

        //public GPSPoint GetGPSData(DataGridViewRow row)
        //{
        //    string name = row.Cells["Tên"].Value.ToString();
        //    DateTime localTime = DateTime.Parse(row.Cells["Thời gian"].Value.ToString());
        //    double lon = double.Parse(row.Cells["Kinh độ"].Value.ToString());
        //    double lat = double.Parse(row.Cells["Vĩ độ"].Value.ToString());
        //    double alt = double.Parse(row.Cells["Độ cao"].Value.ToString());
        //    double speed = double.Parse(row.Cells["Tốc độ"].Value.ToString());
        //    double angle = double.Parse(row.Cells["Hướng"].Value.ToString());
        //    GPSPoint obj = new GPSPoint(formMain.mapControl.Map, name, localTime, lon, lat, alt, speed, angle);

        //    return obj;
        //}

        public GPSPoint GetGPSData(DataRow row)
        {
            string name = row["Name"].ToString();
            DateTime localTime = DateTime.Parse(row["LocalTime"].ToString());
            double lon = double.Parse(row["Lon"].ToString());
            double lat = double.Parse(row["Lat"].ToString());
            double alt = double.Parse(row["Alt"].ToString());
            double speed = double.Parse(row["Speed"].ToString());
            double angle = double.Parse(row["Angle"].ToString());
            GPSPoint obj = new GPSPoint(formMain.mapControl.Map, name, localTime, lon, lat, alt, speed, angle);

            return obj;
        }

        public void AxMap_DrawUserLayer(object sender, AxMapXLib.CMapXEvents_DrawUserLayerEvent e)
        {
            Graphics g = Graphics.FromHdc(new IntPtr(e.hOutputDC));
            List<PointF> dataList = new List<PointF>();
            for (int i = 0; i < dataGridViewTestGPSData.Rows.Count; i++)
            {
                GPSPoint gpsdata = GetGPSData(dbTestGPS.Rows[i]);
                gpsdata.Draw(g, Color.Blue);
                dataList.Add(gpsdata.GetPoint());
            }

            if (checkBoxShowRoutes.Checked)
            {
                Pen pen = new Pen(Color.Red);
                g.DrawLines(pen, dataList.ToArray());
                pen.Dispose();
            }

            if (currGPSData != null)
            {
                currGPSData.Draw(g, Color.Red);
            }
        }

        private void dataGridViewTestGPSData_SelectionChanged(object sender, EventArgs e)
        {
            //Nếu không phải đang tạo dữ liệu test
            if (!isStartTestGPSData)
            {
                if (dataGridViewTestGPSData.CurrentRow != null)
                {
                    currGPSData = GetGPSData(dbTestGPS.Rows[dataGridViewTestGPSData.CurrentRow.Index]);
                    //formMain.mapControl.Map.ZoomTo(formMain.mapControl.Map.Zoom, currGPSData.LonGPS, currGPSData.LatGPS);
                    Graphics g = formMain.mapControl.Map.CreateGraphics();
                    currGPSData.Draw(g, Color.Red);
                }
            }
        }

        private void checkBoxShowRoutes_CheckedChanged(object sender, EventArgs e)
        {
            formMain.mapControl.Map.Refresh();
        }
    }
}